import { Component, OnInit, ViewEncapsulation }       from '@angular/core';
import { Title }                                      from '@angular/platform-browser';
import { DialogService, PickerService, ToastService } from "ngx-weui";
import { DATA }                                       from '../cn';
import { User }                                       from "../../shared/model/user-model";
import { ActivatedRoute, Router }                     from "@angular/router";
import { WxResourceService }                          from "../../shared/service/wx-resource-service";
import { HttpClient }                                 from "@angular/common/http";
import { Result }                                     from "../../shared/model/result";

@Component({
    selector     : 'reseller-signup',
    templateUrl  : './reseller-signup.component.html',
    styleUrls    : [ './reseller-signup.component.less' ],
    encapsulation: ViewEncapsulation.None,
})
export class ResellerSignupComponent implements OnInit
{

    user: User = new User();

    cityData: any = DATA;
    private code: any;

    constructor( private srv: PickerService,
                 private titleService: Title,
                 private route: ActivatedRoute,
                 private router: Router,
                 private wxService: WxResourceService,
                 private http: HttpClient,
                 public toastService: ToastService,
                 private dialog: DialogService )
    {
        titleService.setTitle("申请")
    }

    ngOnInit()
    {
        let openId = sessionStorage.getItem("openId");
        if ( !openId )
        {
            this.code = this.route.snapshot.queryParams["code"];
            if ( !this.code )
            {
                this.getCode()
            }
            else
            {
                this.wxService.getOpenId(this.code).subscribe()
            }
        }
    }

    onSave()
    {
        let openId = sessionStorage.getItem("unionId");
        if ( openId )
        {
            this.toastService.show("提交中", 0, null, "loading");
            this.user.unionId = openId;
            this.user.activated = false;
            this.http.post<Result<void>>(`api/wx/reseller-signup`, this.user).subscribe(value =>
            {
                if ( value.success )
                {
                    this.toastService.destroyAll();
                    this.router.navigateByUrl("/reseller/success")
                }
                else
                {
                    this.toastService.destroyAll();
                    this.dialog.show({
                        title  : '错误',
                        skin   : 'android',
                        content: value.msg,
                    }).subscribe(() =>
                    {

                    })
                }
            }, error =>
            {
                this.toastService.destroyAll();
                this.dialog.show({
                    title  : '错误',
                    skin   : 'android',
                    content: "服务器错误，请联系管理员",
                }).subscribe(() =>
                {

                })
            })
        }
    }

    cityChange( e: any )
    {

    }

    private getCode()
    {
        let currentUrl = encodeURIComponent(window.location.href);
        console.log(currentUrl);
        window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8967c70393c474ea&redirect_uri=${ currentUrl }&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
    }
}
